﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using FluentValidation.Results;
using Microhard.Permission.Interface;
using Microhard.Core;
using Microhard.Core.DBUtility;


namespace Microhard.Permission.Validator
{
    public class PermissionValidator : AbstractValidator<Microhard.Permission.Models.Permission>
    {
        public PermissionValidator()
        {
            IFuncAuthorization itef = IoC.Resolve<IFuncAuthorization>();
            RuleSet("Insert", () =>
               {
                   RuleFor(u => u.ResID).NotEmpty().WithMessage("请选择资源").When(u => u.PermissionID == Guid.Empty);
                   Custom(u => {
                       if (u.PermissionID == Guid.Empty)
                       {
                           if (u.AssignOperations == null || u.AssignOperations.Count == 0 || u.AssignOperations.First().OperationID== Guid.Empty)
                           {
                               return new ValidationFailure("NeedOperations", "请选择操作");
                           }
                       }
                       return null;
                   });
                   Custom(u =>
                   {
                       if (u.PermissionID == Guid.Empty)
                       {
                           if (u.OwnerID == Guid.Empty)
                           {
                               return new ValidationFailure("NeedOwner", "请选择用户或角色");
                           }
                       }
                       return null;
                   });
                   Custom(u =>
                   {

                       if (u.PermissionID == Guid.Empty)
                       {
                           if (itef.IsDuplicate(u))
                           {
                               return new ValidationFailure("Duplicate", "此用户已经拥有此权限");
                           }
                       }
                       return null;
                   });
                   Custom(u =>
                   {
                       IDataCollectionAuthorization ida = IoC.Resolve<IDataCollectionAuthorization>();
                       if (u.PermissionID == Guid.Empty)
                       {
                           if (ida.IsDuplicate(u.PermissionID ,u.Scope))
                           {
                               return new ValidationFailure("DuplicateDataCollection", "此用户已经拥有此数据权限");
                           }
                       }
                       return null;
                   });
                   Custom(u =>
                   {
                       if (u.PermissionID == Guid.Empty)
                       {
                           if (u.Scope == (int)DataCollectionScope.Specific)
                           {
                               if (u.SpecificScopes == null || u.SpecificScopes.Count == 0)
                               {
                                   return new ValidationFailure("needSpcScopes", "请选择组内权限");
                               }
                           }
                       }
                       return null;
                   });
               });
        }
    }
}
